// 全局语言选项 zh(中)/en(英)
let _lang = localStorage.getItem('lang')
let language = _lang ? _lang : 'en';
if (!_lang) {
  localStorage.setItem('lang', 'en')
}

// 简体中文
const zh = {
  lang: '中',
  // topBar相关
  topBar1: '首页',
  topBar2: '公司产品',
  topBar3: '联系我们',
  topBar4: '职位招聘',
  // 首页相关
  home1: '企业简介',
  home2: 'HelloApp是一家总部位于新加坡的手机应用开发和营销公司，致力于为全球用户提供创新的互联网体验。我们专注于为各种移动平台（包括iOS和Android）开发高质量的应用程序。',
  home3: '我们的团队',
  home4: '我们的团队在不同领域开展多个项目，包括但不限于娱乐、生产力工具和互联网服务。我们年轻、富有创意和热情洋溢的开发人员拥有丰富的经验和专业知识，使我们能够为客户提供最佳解决方案。',
  home5: '我们的目标',
  home6: '我们的目标是通过创新和卓越的工作为客户提供优质服务。我们致力于为客户需求提供最佳解决方案，从头到尾与他们紧密合作。',
  home7: '高质量',
  home8: '在HelloApp，我们致力于紧跟行业最新趋势和技术。我们的专家团队致力于交付高质量的工作，超越客户的期望。我们对自己的工作充满激情，致力于帮助客户取得成功。',
  // 公司产品
  company1: 'WeRead是我们公司的一款手机应用程序，致力于为广大小说爱好者提供丰富的阅读体验。该应用涵盖了多种主题，包括奇幻、武侠、言情、都市、校园等等，应有尽有。我们每天都会更新书库，以便读者在几秒钟内就能阅读到最新章节，为读者带来极致的便利。',
  company2: 'WeRead不仅提供原创小说，而且还有奖励金币，让用户在阅读过程中无需花费任何费用。应用根据用户的阅读喜好进行个性化推荐，并提供高质量的书单，总有一款作品能打动用户的心。此外，用户还可以通过完成日常任务来获得金币奖励。',
  company3: '总之，WeRead是一款面向各类小说爱好者的手机应用程序，具备丰富的功能和全球广泛的用户群体。我们相信，WeRead的产品将是我们公司众多手机应用的亮点之一。',
  // 联系我们
  contact1: '联系我们',
  contact2: '企业名称',
  contact3: '注册地址',
  contact4: '企业税号',
  contact5: '办公的地址为',
  contact6: '联系电话',
  contact7: '手机',
  contact8: '联系邮箱',
  // job offers
  offers1: '在招职位',
  offers2: '立即申请',
  offers3: '扫码添加联系人',
}

// 英语
const en = {
  lang: 'EN',
  // topBar相关
  topBar1: 'Home',
  topBar2: "company's product",
  topBar3: 'Contact Us',
  topBar4: 'Job Offers',
  // 首页相关
  home1: 'COMPANY PROFILE',
  home2: 'HelloApp is a mobile application development and marketing company headquartered in Singapore, dedicated to providing innovative Internet experience to users around the world. We specialize in developing high-quality applications for various mobile platforms, including iOS and Android.',
  home3: 'OUR TEAM',
  home4: 'Our teams work on multiple projects in different areas including but not limited to entertainment, productivity tools and internet services. The experience and expertise of our young, creative and passionate developers enables us to provide the best solutions to our customers.',
  home5: 'OUR GOAL',
  home6: "Our aim is to provide quality service to our clients through innovation and excellence in work. We are committed to providing the best solution for our clients' needs, working closely with them from start to finish.",
  home7: 'HIGH QUALITY',
  home8: 'At HelloApp, we strive to stay abreast of the latest trends and technologies in the industry. Our expert team is dedicated to delivering high quality work that exceeds client expectations. We are passionate about what we do and are committed to helping our clients succeed.',
  // 公司产品
  company1: 'WeRead is a mobile application of our company, dedicated to providing a rich reading experience for fiction lovers. The app covers a variety of themes, including fantasy, martial arts, romance, urban, campus, and more. We update the library every day so that readers can read the latest chapters within seconds, bringing ultimate convenience to readers.',
  company2: "WeRead not only provides original novels, but also rewards gold coins, allowing users to spend nothing during the reading process. The app makes personalized recommendations based on the user's reading preferences and provides a high-quality book list. There is always a work that can touch the user's heart. In addition, users can also get gold coin rewards by completing daily tasks.",
  company3: "In short, WeRead is a mobile application for all kinds of novel lovers, with rich functions and a wide user group around the world. We believe that WeRead's products will be one of the highlights of our company's many mobile phone applications.",
  // 联系我们
  contact1: 'CONTACT US',
  contact2: 'Company Name',
  contact3: 'Registered Address',
  contact4: 'Enterprise tax number',
  contact5: 'The office address is',
  contact6: 'Contact number',
  contact7: 'mobile phone',
  contact8: 'Contact email',
  // job offers
  offers1: 'JOB OPENINGS',
  offers2: 'Apply Immediately',
  offers3: 'Scan code to add contacts',
}

init();

/**
 * 初始函数
 */
function init() {
  changeLang();
  var orignalSetItem = localStorage.setItem;
  localStorage.setItem = function(key, newValue){
    var setItemEvent = new Event("setItemEvent");
    setItemEvent.key = key;
    window.dispatchEvent(setItemEvent);
    orignalSetItem.apply(this, arguments);
  };
}

/**
 * 改变语言
 */
function changeLang() {
  const elements = document.querySelectorAll('[lang-attr]');
  for (let i = 0; i < elements.length; i++) {
    const currItem = elements[i];
    const currValue = currItem.getAttribute('lang-attr');
    if (language == 'zh') {
      currItem.innerHTML = zh[currValue];
    } else {
      currItem.innerHTML = en[currValue];
    }
  }
}

/**
 * 设置语言
 * @param {zh | en} lang 语言值
 * @returns 
 */
function setLang(lang) {
  if (lang === language) return
  localStorage.setItem('lang', lang);
  language = lang;
  changeLang();
}

// 点击语言项，全局设置语言
const langOptions = document.querySelectorAll('[option-lang]');
for (let i = 0; i < langOptions.length; i++) {
  const currItem = langOptions[i];
  const currValue = currItem.getAttribute('option-lang');
  if (currValue == 'zh') {
    currItem.addEventListener('click', function() {
      setLang('zh');
      resizeContentHeight()
    });
  } else {
    currItem.addEventListener('click', function() {
      setLang('en');
      resizeContentHeight()
    });
  }
}
